package com.example.testrssparser;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.content.Context;
import android.widget.ListView;

public class MainActivity extends FragmentActivity 
	implements ConfirmDialog.ConfirmDialogListener, ErrorDialog.ErrorDialogListener {

	private static final String msg1 = 
			"We have temporary issues of server responding. Do you want to load rss feed from local storage?";
	//private static final String msg2 = "Connection problem.";
	private static final String msg3 = "SD card not found.";
	//private static final String msg4 = "Connection problem and SD card not found";
	private static final String msg5 = "Not correct data from server.";
	private static final String msg6 = "Not correct data in the file";
	
	private static final String RSS_URL = 
			"http://feeds.foxnews.com/foxnews/latest?format=xml";
	private static final String FILE_NAME = "rss.xml";
	
	ListView mainListView;
	FeedService<Message> service;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        service = new FeedService<Message>(
        		RSS_URL, FILE_NAME, 
        		getExternalFilesDir(null), 
        		new XmlPullFeedParser());
        task1.execute();
    }
    
    @Override
	public void onDialogCashClick(DialogFragment dialog) {
		try {
			if (!service.isCard()) {
				showError(msg3);
				return;
			}
			
			List<Message> values = service.readFile();
			if (values != null) {
	    		view(values);
	    	} else {
	    		showError(msg6);
	    	}
		} catch (CustomParseException e) {
			showError(msg6);
		} catch (CardException e) {
			showError(msg3);
		}
	}
    
    @Override
	public void onDialogErrorClick(DialogFragment dialog) {
		// TODO Auto-generated method stub
		MainActivity.this.finish();
	}
	
	private void view(List<Message> values) {
    	mainListView = (ListView) findViewById(R.id.mainListView);
		mainListView.setAdapter(new CustomArrayAdapter(this, values));	
    }
    
    private void showError(String msg) {
    	DialogFragment edialog = new ErrorDialog(msg);
    	edialog.show(getSupportFragmentManager(), "error");
    }
    
    private void showConfirm(String msg) {
    	DialogFragment dialog = new ConfirmDialog(msg);
    	dialog.show(getSupportFragmentManager(), "confirm");
    }
    
	AsyncTask<String, Integer, List<Message>> task1 = new AsyncTask<String, Integer, List<Message>>() {
		
		private String err = null;

		@Override
		protected List<Message> doInBackground(String... params) {
			// publishProgress(10);
			List<Message> values = null;
			ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
	        
			try {
	        	values = service.readHttp(conMgr);
		    } catch (CustomParseException e) {
		    	err = msg5;
			} catch (CardException e) {
				err = msg3;
			}
	        return values;
		}
		
		@Override
		protected void onPostExecute(java.util.List<Message> values) {
			if (err != null) {
				showError(err);
				err = null;
			} else {
				if (values != null) {
		        	view(values);
		        } else {
		        	showConfirm(msg1);
		        }
			}
		};
	};

}
